if (!Util.isValid(QSAPI)) {
    var QSAPI = {
        Widget : {}
    };
} else if (!QSAPI.Widget) {
    QSAPI.Widget = {};
}
QSAPI.Widget.WatchlistSummary=function(container,config,callback){
	var qslan=QS_Lang.watchlist;
	this.container=container;
	this.config={
		height:35,
        needShowDate:false
	};
	this.callback={};
	if(config){
		$.extend(this.config,config);
	}
	if(callback){
        $.extend(this.callback,callback);
    }
	this.html='<div class="rtq-wl-summary" style="height:' + this.config.height + 'px"><div class="rtq-wl-sum-container clearfix">' +
        '<div class="rtq-wl-sum-r"><div class="rtq-wl-sum-rt"></div><div class="rtq-wl-sum-rb"></div></div>' +
        '<div class="rtq-wl-sum-l"><div class="rtq-wl-sum-lt">'+qslan.title+'<a class="rtq-wl-sum-refresh" href="javascript:void(0)">'+qslan.refresh+'</a></div><div class="rtq-wl-sum-lb"></div></div>' +
        '</div></div>';
    this.el=null;
	this.subEl=null;
    this.disableRefresh=false;
	this.init();
};
QSAPI.Widget.WatchlistSummary.prototype={
	init:function(){
		this.el=$(this.html).appendTo(this.container);
		this.subEl={
            'lt': this.el.find('.rtq-wl-sum-lt'),
            'lb': this.el.find('.rtq-wl-sum-lb'),
            'rt': this.el.find('.rtq-wl-sum-rt'),
            'rb': this.el.find('.rtq-wl-sum-rb')			
		};
        this.refreshButton = this.subEl['lt'].find('.rtq-wl-sum-refresh');
		this._initEvent();
        this.hide();
	},
	_initEvent:function(){
		var self=this;
		this.refreshButton.bind('click.watchlistSummary',function(e){
			if(Util.isFunction(self.callback.onClickRefresh)&&!self.disableRefresh){
				self.callback.onClickRefresh();
			}
        });
	},
	show:function(){
		this.el.show();
	},
	hide:function(){
		this.el.hide();
	},
    disableRefreshButton:function(){
        this.refreshButton.addClass("rtq-ddl-disable");
        this.disableRefresh=true;
    },
    ableRefreshButton:function(){
        this.refreshButton.removeClass("rtq-ddl-disable");
        this.disableRefresh=false;
    },
	updateSummary:function(o){
        var qslan=QS_Lang.watchlist;                 
        this.el.find('.rtq-wl-sum-container').show();     
        if (!Util.isValid(o,true,true,true)) {
            this.subEl['lb'][0].innerHTML = String.format(qslan["lastupdated"],'--');
            this.subEl['rt'][0].innerHTML = String.format(qslan["tccontent"],'0.00','0.00','0.00%');
            this.subEl['rb'][0].innerHTML = String.format(qslan["mvcontent"],'0.00','0.00','0.00%');
        }else {
            var tc=o["TC"]||'0.00',pl=o["P/L"]||'0.00',plp=o["P/Lpc"]||'0.00',mv=o["MarketValue"]||'0.00',dpl=o["DP/L"]||'0.00',dplp=o["DP/Lpc"]||'0.00';
            if(this.config.needShowDate){
                this.subEl['lb'][0].innerHTML = String.format(qslan["lastupdated"],o.updatedDate||'--');
            }
            this.subEl['rt'][0].innerHTML = String.format(qslan["tccontent"],tc,pl,plp);
            this.subEl['rb'][0].innerHTML = String.format(qslan["mvcontent"],mv,dpl,dplp);
        }
	},
	emptySummary:function(){
		this.subEl['lb'][0].innerHTML ='';
        this.subEl['rt'][0].innerHTML ='';
        this.subEl['rb'][0].innerHTML ='';
        this.el.find('.rtq-wl-sum-container').hide();
	},
	clear:function(){
		this.subEl['lt'].unbind('.watchlistSummary');
		for (var p in this) {
            if (!p || p == 'clear') {
                continue;
            }
            this[p] = null;
            delete this[p];
        }
	}
};
